package jmine.tec.hydra.services.incluir;

import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.hydra.domain.HydraSystem;
import jmine.tec.hydra.domain.filter.HydraFilter;
import jmine.tec.hydra.domain.filter.HydraFilterType;
import jmine.tec.script.persistent.domain.Script;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.Output;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;

/**
 * Serviço para inclusão de um filtro de integração.
 * 
 * @author piercio
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class HydraFilterService implements ServiceFiller<HydraFilter> {

    public static final String IDENTIFICADOR = "Identificador";

    public static final String MNEMONICO = "Mnemonico";

    public static final String ACTION = "Ação";

    public static final String SYSTEM = "System";

    public static final String SCRIPT = "Script";

    public static final String FILTER_TYPE = "Tipo de Filtro";

    public static final String ACTIVE = "Ativo";

    public static final String DATABASE_ID = "Database ID";

    // Injected
    private PackEntityPersistenceHelper helper;

    // Obrigatório
    private String mnemonico;

    private HydraSystem system;

    private Script script;

    private HydraFilterType filterType;

    // Opcionais
    private String action;

    private boolean active;

    private Long databaseID;

    /**
     * Executa o serviço e cria um {@link HydraFilter}
     * 
     * @return o sistema de integração.
     * @throws PackException PackException
     */
    @Execution
    @Output(propertyName = IDENTIFICADOR)
    public HydraFilter executeService() throws PackException {
        HydraFilter filter = this.helper.findOrCreate(HydraFilter.class, this.databaseID, this.mnemonico);
        filter.setMnemonico(this.mnemonico);
        filter.setAction(this.action);
        filter.setSystem(this.system);
        filter.setScript(this.script);
        filter.setFilterType(this.filterType);
        filter.setActive(this.active);
        this.helper.save(filter, this.databaseID);
        return filter;
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, HydraFilter entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.put(bean, MNEMONICO, entity.getMnemonico());
        ExportUtils.put(bean, SYSTEM, referenceMap, entity.getSystem(), entity.getSystem().getMnemonico());
        ExportUtils.put(bean, SCRIPT, entity.getScript().getMnemonico());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
        ExportUtils.put(bean, IDENTIFICADOR, referenceMap.addReference(entity, "hydraFilter"));
        ExportUtils.put(bean, ACTION, entity.getAction());
        ExportUtils.put(bean, FILTER_TYPE, entity.getFilterType());
        ExportUtils.put(bean, ACTIVE, entity.isActive());
    }

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }

    /**
     * @param mnemonico o valor para o campo mnemonico
     */
    @Input(fieldName = MNEMONICO)
    public void setMnemonico(String mnemonico) {
        this.mnemonico = mnemonico;
    }

    /**
     * @param action o valor para o campo action
     */
    @Input(fieldName = ACTION, required = false)
    public void setAction(String action) {
        this.action = action;
    }

    /**
     * @param system o valor para o campo system
     */
    @Input(fieldName = SYSTEM)
    public void setSystem(HydraSystem system) {
        this.system = system;
    }

    /**
     * @param script o valor para o campo script
     */
    @Input(fieldName = SCRIPT)
    public void setScript(Script script) {
        this.script = script;
    }

    /**
     * @param filterType o valor para o campo filterType
     */
    @Input(fieldName = FILTER_TYPE)
    public void setFilterType(HydraFilterType filterType) {
        this.filterType = filterType;
    }

    /**
     * @param active o valor para o campo active
     */
    @Input(fieldName = ACTIVE, required = false, defaultValue = "false")
    public void setActive(boolean active) {
        this.active = active;
    }

    /**
     * @param databaseID the databaseID to set
     */
    @Input(fieldName = DATABASE_ID, required = false)
    public void setDatabaseID(Long databaseID) {
        this.databaseID = databaseID;
    }
}
